package com.chuji.crmcj.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bumptech.glide.Glide;
import com.chuji.crmcj.R;
import com.chuji.crmcj.adapter.SalesAdapter;
import com.chuji.crmcj.bean.AgreeOrderInfo;
import com.chuji.crmcj.bean.CancelUserListInfo;
import com.chuji.crmcj.bean.MessageAllUserInfo;
import com.chuji.crmcj.utils.SPUtils;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.utils.UrlUtils;
import com.chuji.crmcj.utils.VolleyUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * Created by w.pitt on 2016/1/22.
 */
public class AddRecPeoAct extends BaseActivity implements View.OnClickListener {
    //返回
    private ImageView mIv_Back;
    //保存
    private LinearLayout mLl_save;
    //选中全部人员
    private RelativeLayout Rl_total;
    private CheckBox cb_total;
    //选中销售一组
    private RelativeLayout Rl_first_team;
    private CheckBox cb_first_team;
    //显示
    private ListView mLv_saler;
    //适配器
    private MessageAllUserAdapter adapter;
    //驳回人员列表存储
    List<MessageAllUserInfo.ApiParamObjEntity> messageAllUserData;
    //驳回人员列表网络数据
    private MessageAllUserInfo messageAllUserInfo;
    //请求码
    private int resultCode = 0;
    //集合
    List<MessageAllUserInfo.ApiParamObjEntity> list= new ArrayList<MessageAllUserInfo.ApiParamObjEntity>();
    //用户ID
    String UserID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_add_notice_peo);
        mIv_Back = (ImageView) findViewById(R.id.iv_Back);
        mLl_save = (LinearLayout) findViewById(R.id.ll_save);
        Rl_total = (RelativeLayout) findViewById(R.id.Rl_total);
        Rl_first_team = (RelativeLayout) findViewById(R.id.Rl_first_team);
        cb_total = (CheckBox) findViewById(R.id.cb_total);
        cb_first_team= (CheckBox) findViewById(R.id.cb_first_team);
        mLv_saler = (ListView) findViewById(R.id.lv_saler);
        //获取数据
        getSales();
        //点击事件
        mIv_Back.setOnClickListener(this);
        mLl_save.setOnClickListener(this);
        Rl_total.setOnClickListener(this);
        Rl_first_team.setOnClickListener(this);
        //保存按钮不可点击
        mLl_save.setEnabled(false);
    }
    @Override
    protected void initView() {

    }
    public void getSales(){
        UserID=SPUtils.getString(UIUtils.getContext(),"UserID","");
        String strURL = String.format(UrlUtils.GET_ALL_USER+"&UserID="+UserID);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, strURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                messageAllUserInfo = JSON.parseObject(response, MessageAllUserInfo.class);
                //第一次
                if (messageAllUserData == null) {
                    messageAllUserData = new ArrayList<MessageAllUserInfo.ApiParamObjEntity>();
                }
                messageAllUserData.addAll(messageAllUserInfo.getApiParamObj());
                UIUtils.runInMainThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter=new MessageAllUserAdapter(UIUtils.getContext(),messageAllUserData);
                        mLv_saler.setAdapter(adapter);
                        // 全选按钮的回调接口
                        Rl_total.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if(cb_total.isChecked()==false) {
                                    cb_total.setChecked(true);
                                    if (cb_total.isChecked()==true) {
                                        // 遍历list的长度，将MyAdapter中的map值全部设为true
                                        for (int i = 0; i < messageAllUserData.size(); i++) {
                                            adapter.getIsSelected().put(i, true);
                                            cb_first_team.setChecked(true);
                                        }
                                    } else {
                                        // 遍历list的长度，将MyAdapter中的map值全部设为true
                                        for (int i = 0; i < messageAllUserData.size(); i++) {
                                            adapter.getIsSelected().put(i, false);
                                            cb_first_team.setChecked(false);
                                        }
                                    }
                                }else{
                                    cb_total.setChecked(false);
                                    if (cb_total.isChecked()==true) {
                                        // 遍历list的长度，将MyAdapter中的map值全部设为true
                                        for (int i = 0; i < messageAllUserData.size(); i++) {
                                            adapter.getIsSelected().put(i, true);
                                            cb_first_team.setChecked(true);
                                        }
                                    } else {
                                        // 遍历list的长度，将MyAdapter中的map值全部设为true
                                        for (int i = 0; i < messageAllUserData.size(); i++) {
                                            adapter.getIsSelected().put(i, false);
                                            cb_first_team.setChecked(false);
                                        }
                                    }
                                }
                            }
                        });
                        //选中其中一组
                        Rl_first_team.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if(cb_first_team.isChecked()==false) {
                                    cb_first_team.setChecked(true);
                                    if (cb_first_team.isChecked()==true) {
                                        // 遍历list的长度，将MyAdapter中的map值全部设为true
                                        for (int i = 0; i < messageAllUserData.size(); i++) {
                                            adapter.getIsSelected().put(i, true);
                                            cb_first_team.setChecked(true);
                                        }
                                    } else {
                                        // 遍历list的长度，将MyAdapter中的map值全部设为true
                                        for (int i = 0; i < messageAllUserData.size(); i++) {
                                            adapter.getIsSelected().put(i, false);
                                            cb_first_team.setChecked(false);
                                        }
                                    }
                                }else{
                                    cb_first_team.setChecked(false);
                                    if (cb_first_team.isChecked()==true) {
                                        // 遍历list的长度，将MyAdapter中的map值全部设为true
                                        for (int i = 0; i < messageAllUserData.size(); i++) {
                                            adapter.getIsSelected().put(i, true);
                                            cb_first_team.setChecked(true);
                                        }
                                    } else {
                                        // 遍历list的长度，将MyAdapter中的map值全部设为true
                                        for (int i = 0; i < messageAllUserData.size(); i++) {
                                            adapter.getIsSelected().put(i, false);
                                            cb_first_team.setChecked(false);
                                        }
                                    }
                                }
                            }
                        });
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                System.out.println("失败" + error);
                UIUtils.showToastSafe("请检查网络");
            }
        });
        // Add the request to the RequestQueue.
        VolleyUtil.getRequestQueue().add(stringRequest);
    }
    @Override
    protected void initData() {
    }

    @Override
    protected void initListener() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_Back:
                finish();
                break;
        }
    }
    class MessageAllUserAdapter extends BaseAdapter {
        private Context context;
        //驳回人员列表存储
        List<MessageAllUserInfo.ApiParamObjEntity> messageAllUserData;
        //驳回人员列表网络数据
        private MessageAllUserInfo messageAllUserInfo;
        // 用来控制CheckBox的选中状况
        private HashMap<Integer, Boolean> isSelected;


        public MessageAllUserAdapter(Context context, List<MessageAllUserInfo.ApiParamObjEntity> messageAllUserData) {
            isSelected = new HashMap<Integer, Boolean>();
            this.context = context;
            this.messageAllUserData=messageAllUserData;
            // 初始化数据
            initDate();
        }
        // 初始化isSelected的数据
        private void initDate() {
            for (int i = 0; i < messageAllUserData.size(); i++) {
                getIsSelected().put(i, false);
            }
        }
        @Override
        public int getCount() {
            return messageAllUserData.size();
        }

        @Override
        public Object getItem(int position) {
            return messageAllUserData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.dialog_reject_to_sales, null);
                holder = new ViewHolder();
                holder.mLl_genjinfangshi = (LinearLayout) convertView.findViewById(R.id.ll_genjinfangshi);
                holder.mIv_sales_head = (ImageView) convertView.findViewById(R.id.iv_sales_head);
                holder.mTv_device_name = (TextView) convertView.findViewById(R.id.tv_device_name);
                holder.mCb_familycondition = (CheckBox) convertView.findViewById(R.id.cb_familycondition);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            Glide.with(UIUtils.getContext())
                    .load(UrlUtils.ImageIP + messageAllUserData.get(position).getAvatar())
                    .into(holder.mIv_sales_head);
            holder.mTv_device_name.setText(messageAllUserData.get(position).getRealName());

            holder.mLl_genjinfangshi.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if (holder.mCb_familycondition.isChecked() == false) {
                        holder.mCb_familycondition.setChecked(true);
                    } else {
                        holder.mCb_familycondition.setChecked(false);
                    }
                    if (isSelected.get(position)) {
                        isSelected.put(position, false);
                        setIsSelected(isSelected);
                    } else {
                        isSelected.put(position, true);
                        setIsSelected(isSelected);
                    }
                    list.add(messageAllUserData.get(0));
                    list.add(messageAllUserData.get(1));
                    UIUtils.runInMainThread(new Runnable() {
                        @Override
                        public void run() {
                            mLl_save.setEnabled(true);
                            mLl_save.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    //传值
                                    Intent intent = new Intent(UIUtils.getContext(),NewNoticeActivity.class);
                                    Bundle bundle = new Bundle();
                                    bundle.putSerializable("sales", (Serializable) list);//序列化,要注意转化(Serializable)
                                    intent.putExtras(bundle);//发送数据
                                    // 设置结果，并进行传送
                                    setResult(resultCode, intent);
                                }
                            });
                        }
                    });
                }
            });
            // 根据isSelected来设置checkbox的选中状况
            holder.mCb_familycondition.setChecked(getIsSelected().get(position));
            // 全选按钮的回调接口
            cb_total.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        // 遍历list的长度，将MyAdapter中的map值全部设为true
                        for (int i = 0; i < messageAllUserData.size(); i++) {
                            isSelected.put(i, true);
                            setIsSelected(isSelected);
                            cb_first_team.setChecked(true);
                            adapter.notifyDataSetChanged();
                        }
                    } else {
                        // 遍历list的长度，将MyAdapter中的map值全部设为true
                        for (int i = 0; i < messageAllUserData.size(); i++) {
                            isSelected.put(i, false);
                            cb_first_team.setChecked(false);
                            adapter.notifyDataSetChanged();
                        }
                    }
                    setIsSelected(isSelected);
                }
            });
            // 选中销售一组的回调接口
            cb_first_team.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        // 遍历list的长度，将MyAdapter中的map值全部设为true
                        for (int i = 0; i < messageAllUserData.size(); i++) {
                            isSelected.put(i, true);
                            cb_first_team.setChecked(true);
                            adapter.notifyDataSetChanged();
                        }
                    } else {
                        // 遍历list的长度，将MyAdapter中的map值全部设为true
                        for (int i = 0; i < messageAllUserData.size(); i++) {
                            isSelected.put(i, false);
                            cb_first_team.setChecked(false);
                            adapter.notifyDataSetChanged();
                        }
                    }
                    setIsSelected(isSelected);
                }
            });
            return convertView;
        }

        public HashMap<Integer, Boolean> getIsSelected() {
            return isSelected;
        }

        public void setIsSelected(HashMap<Integer, Boolean> isSelected) {
            this.isSelected = isSelected;
        }

        class ViewHolder {
            private LinearLayout mLl_genjinfangshi;
            private ImageView mIv_sales_head;
            private TextView mTv_device_name;
            private CheckBox mCb_familycondition;
        }
    }
}
